package com.gsww.vue.service.sys.impl;


import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gsww.vue.dao.sys.SysAccountDao;
import com.gsww.vue.dao.sys.SysRoleAcctRelDao;
import com.gsww.vue.dao.sys.SysRoleDao;
import com.gsww.vue.entity.sys.SysAccount;
import com.gsww.vue.entity.sys.SysRole;
import com.gsww.vue.entity.sys.SysRoleAcctRel;
import com.gsww.vue.service.BasicService;
import com.gsww.vue.service.sys.SysAccountService;
@Service
@Transactional
public class SysAccountServiceImpl extends BasicService implements SysAccountService {
	@Autowired
	private JdbcTemplate jdbcTemplate;
	@Autowired 
	private SysRoleAcctRelDao sysRoleAcctRelDao;
	@Autowired
	private SysRoleDao sysRoleDao;
	@Autowired
	private SysAccountDao sysAccountDao;
		
	@Override
	public Map<String, Object> getAccountList(String userName, String password,String pageNo,String pageSize) throws Exception {
		List<Object> paramList = new ArrayList<Object>();
		StringBuilder sb = new StringBuilder();
		sb.append("select t.USER_ACCT_ID,t.LOGIN_ACCOUNT,t.USER_NAME,t.USER_TELE,");
		sb.append("t.CREATE_TIME,t.USER_STATE,");
				//+ "(case t.user_state when '1' then '启用' when '0' then '停用' else '' end) user_state,"
		sb.append("(select dept_name from sys_department d where d.dept_id=t.dept_id) as DEPT_NAME from sys_account t where 1=1 ");
		if(StringUtils.isNotBlank(userName)){
			sb.append(" and t.user_name like ?");
			paramList.add("%"+userName+"%");
		}
		sb.append(" and t.user_state!='0' order by t.create_time desc ");
		Map<String, Object> map = super.page( sb.toString(),paramList, pageNo, pageSize);
		//map.put("head", super.readJson(riverLake).get(tableName));
		return map;
		//return jdbcTemplate.queryForList(sb.toString(),paramList.toArray());
	}
	@Override
	public List<SysRole> findAccountRoleList(String userId) throws Exception {
		List<SysRole> list=new ArrayList<SysRole>();
		List<SysRoleAcctRel> roleAcctRelList=new ArrayList<SysRoleAcctRel>();
		roleAcctRelList = sysRoleAcctRelDao.findByUserAcctId(userId);
		if(roleAcctRelList.size()>0){
			for (SysRoleAcctRel sysRoleAcctRel : roleAcctRelList) {
				SysRole sysRole=sysRoleDao.findByRoleId(sysRoleAcctRel.getRoleId());
				list.add(sysRole);
			}
		}
		return list;
	}
	@Override
	public SysAccount findByKey(String pk) throws Exception {
		SysAccount sysAccount=sysAccountDao.findByUserAcctId(pk);
		return sysAccount;
	}
	@Override
	public SysAccount save(SysAccount sysAccount) throws Exception {
		return sysAccountDao.save(sysAccount);
	}
	/**
	 * 删除用户角色中间表数据
	 * @param userInfo
	 * @throws Exception
	 */
	public void deleteAccountRole(SysAccount entity) throws Exception{
		if(entity!=null){
			List<SysRoleAcctRel> list=sysRoleAcctRelDao.findByUserAcctId(entity.getUserAcctId());
			if(list.size()>0){
				for (SysRoleAcctRel sysRoleAcctRel : list) {
					sysRoleAcctRelDao.delete(sysRoleAcctRel);
				}			
			}
		}
	}
	
	/**
	 * 保存用户角色关系表
	 * @param userId
	 * @param roleId
	 * @throws Exception
	 */
	@Override
	public void saveUserRole(String userId,String roleId) throws Exception{
		SysRoleAcctRel sysRoleAcctRel=new SysRoleAcctRel();
		sysRoleAcctRel.setUserAcctId(userId);
		sysRoleAcctRel.setRoleId(roleId);
		sysRoleAcctRelDao.save(sysRoleAcctRel);
	}
	@Override
	public List<Map<String, Object>> getHtml(String id) throws Exception {
		return jdbcTemplate.queryForList("select * from TBL_TEMP where pk_id=?",id);
	}
	

}
